<template>
  <!-- eslint-disable -->
  <div class="columnDiv">
    <div class="leftMenu">
      <div>
        <div class="progName">栏目名</div>
        <div class="allContent" @click="getAllColumns">全部内容</div>
        <el-tree :data="treeData" :props="defaultProps" accordion @node-click="handleNodeClick" style="margin-bottom: 30px" />
      </div>
      <el-button type="primary" size="small" icon="el-icon-plus" class="addNewActicle" @click="handleAddColumn">新增栏目</el-button>
    </div>
    <div class="rightList">
      <div class="topTitle">院校动态二级</div>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="columnsList" style="width: 100%">
        <el-table-column align="center" prop="template" label="栏目模板">
          <template slot-scope="{row}">
            <span>{{ row.template === 1 ? '列表' : '单篇'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="title" label="栏目名称" />
        <el-table-column align="center" prop="image" label="栏目图片">
          <template slot-scope="{row}">
            <img :src="row.image" class="columnImg" alt="">
          </template>
        </el-table-column>
        <el-table-column align="center" prop="external_url" label="外链URL">
          <template slot-scope="{row}">
            <span>{{ (row.external_url && row.external_url.length > 0) ? row.external_url : '无外链' }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="weight" label="显示顺序" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <el-tooltip effect="dark" content="编辑" placement="top-start">
              <span class="btnIcon edit" @click="handleEditColumn(row)"></span>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top-start">
              <span class="btnIcon delete" @click="handleDeleteColumn(row)"></span>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange" 
        @current-change="handleCurrentChange" 
        :current-page.sync="columnsParams.page" 
        :page-sizes="[10,20,30,50]" 
        :page-size="columnsParams.limit" 
        layout="prev, pager, next, total, sizes, jumper"
        :total="total"
        style="margin-top: 30px;"
      />
    </div>
  </div>
</template>

<script>
import { getColumnTrees, getColumnsLists, deleteColumns } from '../../api/informationNetwork'
export default {
  data() {
    return {
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      treeData: [], // 栏目树列表
      loading: false,
      columnsList: [],
      columnsParams: {
        page: 1,
        limit: 10,
        column_id: null
      },
      total: null
    }
  },
  mounted() {
    this.getColumnTrees()
    this.getColumnsLists()
  },
  methods: {
    // 获取栏目树
    getColumnTrees() {
      getColumnTrees().then(res => {
        this.treeData = res
      })
    },
    // 获取栏目列表
    getColumnsLists() {
      this.loading = true
      getColumnsLists(this.columnsParams).then(res => {
        this.columnsList = res.data
        this.total = res.meta.total
      }).catch(() => {
      }).finally(() => {
        this.loading = false
      })
    },
    // 分页
    handleSizeChange(val) {
      this.columnsParams.limit = val
      this.getColumnsLists()
    },
    handleCurrentChange(val) {
      this.columnsParams.page = val
      this.getColumnsLists()
    },
    // 点击树状导航栏
    handleNodeClick(data) {
      this.columnsParams.column_id = data.id
      this.getColumnsLists()
    },
    getAllColumns() {
      this.columnsParams = {
        column_id: null,
        page: 1,
        limit: 10
      }
      this.getColumnsLists()
    },
    // 点击'删除'
    handleDeleteColumn(row) {
      this.$confirm('您是否确认删除该栏目', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteColumns(row.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功！'
          })
          this.columnsParams.page = Math.ceil((this.total - 1) / this.columnsParams.limit)
          this.getColumnsLists()
          this.getColumnTrees()
        }).finally(() => {
          this.loading = false
        })
      }).catch(() => {
      })
    },
    // 新增栏目
    handleAddColumn() {
      this.$router.push({ path: './column/add' })
    },
    handleEditColumn(row) {
      this.$router.push({
        path: './column/edit',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss">
  $defaultColor: #0A2045;
  .btnIcon {
    display: inline-block;
    width: 24px;
    height: 24px;
    cursor: pointer;
    margin-right: 10px;
  }
  // 删除
  .delete {
    background: url('../../assets/btn/icon_delete.png') no-repeat center center;
  }
  .delete:hover {
    background: url('../../assets/btn/icon_delete_s.png') no-repeat center center;
  }
  // 查看
  .edit {
    background: url('../../assets/btn/icon_edit.png') no-repeat center center;
  }
  .edit:hover {
    background: url('../../assets/btn/icon_edit_s.png') no-repeat center center;
  }
  .el-button--primary {
    color: #FFF;
    background-color: #0C66FF;
    border-color: #0C66FF;
  }
  .el-button--small {
    border-radius: 8px;
  }
  .el-button--primary:hover {
    color: #fff;
    background-color: #0951CC;
    border-color: #0951CC;
  }

  .el-tree-node__content {
    color: $defaultColor;
  }

  .el-tree-node:focus>.el-tree-node__content {
    color: #0C66FF;
  }

  .el-table {
    color: $defaultColor;
    font-size: 14px;
    line-height: 19px;
    thead {
      color: $defaultColor;
      tr, th {
        background: #FAFAFB;
      }
    }
  }
  .columnDiv {
    width: 100%;
    display: flex;
    justify-content: flex-start;

    .leftMenu {
      width: 280px;
      padding: 25px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .progName {
        color: $defaultColor;
        font-size: 14px;
        font-weight: bold;
        line-height: 19px;
        margin-bottom: 37px;
      }
      .allContent {
        color: $defaultColor;
        font-size: 14px;
        line-height: 26px;
        padding-left: 26px;
        cursor: pointer;
      }
      .allContent:hover {
        color: #0C66FF;
        background: #FAFAFB;
      }
      .addNewActicle {
        width: 100%;
        font-size: 14px;
        line-height: 17px;
      }
    }

    .rightList {
      flex: 1;
      padding: 19px 25px 13px;
      border-left: 1px solid #ECECF2;

      .topTitle {
        color: $defaultColor;
        font-size: 14px;
        font-weight: bold;
        height: 74px;
        line-height: 74px;
      }
      .columnImg {
        width: 60px;
        height: 60px;
      }
    }

  }
</style>

